import http from './requestConfig'

/**
 * 设设API域
 * @param env
 * @returns {*}
 */
export function setBaseURL (env = null) {
  if (!env) {
    process.env.NODE_ENV === 'production' ? env = 'production' : env = 'development'
  }

  // #ifdef H5
  if (['localhost'].includes(window.location.hostname)) {
    env = 'development'
  }
  if (['test-m.bendiclass.com'].includes(window.location.hostname)) {
    env = 'development'
  }

  if (['m.bendiclass.com'].includes(window.location.hostname)) {
    env = 'production'
  }
  // #endif

  const URL_LIST = {
    development: 'https://test-api.bendiclass.com',
    production: 'https://api.bendiclass.com'
  }

  return URL_LIST[env]
}

function getTrackModel () {
  let isUma = true

  // #ifdef H5
  if (window._hmt) {
    isUma = false
  }
  // #endif

  return isUma
}

/**
 * 基础配置选项
 * 使用：uni.flux.config.tokenKey
 */
const config = {
  isUma: getTrackModel(),
  requestUrl: setBaseURL(),
  appId: 'wx47b74cca2932cd6d',
  tokenKey: 'XIAOSHUBAO-TOKEN',
  mainPage: [
    '/pages/index/index',
    '/pages/lesson/index',
    '/pages/mine/index/index'
  ],
  orderFrom: {
    0: '不存在',
    1: '畅读卡',
    2: '联合卡',
    3: '知识点精讲',
    4: '学习包',
    5: '打赏',
    6: 'PDF',
    7: '听力'
  }
}

// http请求配置项
http.setConfig({
  baseUrl: config.requestUrl,
  fileUrl: config.requestUrl,
  defaultUploadUrl: config.requestUrl,
  header: {
    'content-type': 'application/json;charset=UTF-8'
  },
  timeout: 10000,
  config: {
    // 是否自动提示错误
    isPrompt: true,
    // 是否显示加载动画
    load: false,
    // 是否使用数据工厂
    isFactory: true
  }
})

export {
  config,
  http
}
